<?php
header("content-type:text/html;charset=utf-8");
echo "<title>UP MP4</title>";
function flarr(){
	$arr=scandir("./name/");
	unset($arr[0]);
	unset($arr[1]);
	foreach($arr as $v){
		$fltxt=file("./name/".$v);
		$a=trim($v,".txt");
		$flend[$a]=trim($fltxt[0])." <a href='admin.php?del=".$a."'>删除</a>";
	}
	return $flend;
}

function del($flName){
	if(file_exists("./mp4/".$flName.".mp4")){
		unlink("./mp4/".$flName.".mp4");
	}
	if(file_exists("./name/".$flName.".txt")){
		unlink("./name/".$flName.".txt");
	}
	
}
function upfile(){
	$arr=$_FILES['upfile'];
	if($arr['error']=='0'){
		$yname=$_POST['yname'];
		$cname=$_POST['cname'];
		$filename=$arr['name'];
		$re=move_uploaded_file($arr['tmp_name'], "./mp4/".$yname.".mp4");
		if($re){
			echo "up file success!";
			$handle=fopen("./name/".$yname.".txt", 'w+');
			$w=fwrite($handle, $cname);
			echo " fw:".$w;
		}
	}else{
		echo"up file error!";
	};
}
if($_GET['del']){
	del($_GET['del']);
	$fl=flarr();
}
if($_POST['have']=='have'){
	if($_FILES['upfile']['name']=='' || $_POST['yname']=='' || $_POST['cname']==''){
		echo "<script>";
		echo "alert('文件、英文名和中文名缺一不可');";
//		echo "window.location.href='#';";
		echo "</script>";
	}else{
		$fl=flarr();
		$ynames=array_keys($fl);
		if(in_array($_POST['yname'], $ynames)){
			echo "<script>";
			echo "alert('英文名有重名');";
			echo "window.location.href='#';";
			echo "</script>";
		}else{
			upfile();
		}
	}
}
$fl=flarr();
echo "<pre>";
print_r($fl);
echo "</pre>";
?>
<form action="#" method="post" enctype="multipart/form-data">
	<input type="hidden" name="have" value="have" />
	上传一个MP4文件:<input type="file" name="upfile" id="upfile" value="" /><br />
	取一个英文名:<input type="text" name="yname" value="" /><br />
	取一个中文名:<input type="text" name="cname" value="" /><br />
	<input type="submit" value="上传" />
</form>